import Mock from 'mockjs'

function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse('{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') +
        '"}')
}

let List = []
const count = 200
for (let i = 0; i < count; i++) {
    List.push(
        Mock.mock({
            id: Mock.Random.guid(),
            name: Mock.Random.cname(),
            addr: Mock.mock('@county(true)'),
            'age|18-60': 1,
            birth: Mock.Random.date(),
            sex: Mock.Random.integer(0, 1)
        })
    )
}

export default {
    getUserList: config => {
        const { name, page = 1, limit = 20 } = param2Obj(config.url)
        const mockList = List.filter(user => {
            if (name && user.name.indexOf(name) === -1 && user.addr.indexOf(name) === -1) return false
            return true
        })
        const pageList = mockList.filter((item, index) => index < parseInt(limit) + parseInt(page)
            && index >= parseInt(limit) * (parseInt(page) - 1))

        return {
            code: 20000,
            message: '获取列表成功',
            data: {
                count: mockList.length,
                list: pageList
            }
        }
    },

    createUser: config => {
        console.log(config)
        const { name, addr, age, birth, sex } = JSON.parse(config.body)
        let flag = true
        if (name === '') flag = false
        if (addr === '') flag = false
        if (age === '') flag = false
        if (birth === '') flag = false
        if (sex === '') flag = false
        if (flag) {
            List.unshift({
                id: Mock.Random.guid(),
                name: name,
                addr: addr,
                age: age,
                birth: birth,
                sex: sex,

            })
            return {
                code: 20000,
                message: '添加成功',
                data: {
                    ...List[0]
                }
            }
        }
        else {
            return {
                code: -999,
                message: "添加失败，字段不能为空",
                error: {
                    name: name,
                    addr: addr,
                    age: age,
                    birth: birth,
                    sex: sex,
                }
            }
        }

    },
    
    updateUser: config => {
        const { id, name, addr, age, birth, sex } = JSON.parse(config.body)
        const sex_num = parseInt(sex)
        List.some(u => {
            if (u.id === id) {
                u.name = name
                u.addr = addr
                u.age = age
                u.birth = birth
                u.sex = sex_num
                return true
            }
        })
        return {
            code: 20000,
            message: '编辑成功',
            data: {
                id: id
            }
        }
    },

    deleteUser: config => {
        const { id } = param2Obj(config.url)
        if (!id) {
            return {
                code: -999,
                message: `参数id：{id}不存在`
            }
        } else {
            List = List.filter(u => u.id !== id)
            return {
                code: 20000,
                message: '删除成功',
                data: {
                    id: id
                }
            }
        }
    },

    batchRemove: config => {
        let { ids } = param2Obj(config.url)
        ids = ids.split(',')
        List = List.filter(u => !ids.includes(u.id))
        return {
            code: 20000,
            message: '批量删除成功'
        }
    },




}